//
//  YSWelcomeViewController.swift
//  新浪浪
//
//  Created by mac on 16/2/29.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SDWebImage

class YSWelcomeViewController: UIViewController {

    override func loadView() {
        
    
        view = bgImageView
//        super.loadView()
       
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    private func startAnimation(){
        self.photoImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset (-UIScreen.mainScreen().bounds.height + 100)
            
            make.centerX.equalTo(self.view.snp_centerX)
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
//        UIView.animateWithDuration(2.0, delay: 0, options: <#T##UIViewAnimationOptions#>, animations: <#T##() -> Void#>, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)
//UIView.animateWithDuration(2.0, animations: { () -> Void in
//    
//    }, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)

        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                print("完成之后")
                
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChangRootVc, object: nil)
        }
    }
    
    
    
    //添加控件和约束
    private func setUI(){
        
        view.addSubview(photoImage)
        view.addSubview(nameLabel)
        
//        设置约束
        photoImage.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-150)
            make.centerX.equalTo(view.snp_centerX)
            
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(photoImage.snp_bottom).offset(10)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        let url = NSURL (string: YSUserViewModel.shareInstance.model?.profile_image_url ?? "")
        if let Ourl = url {
            photoImage.sd_setImageWithURL(Ourl,placeholderImage:UIImage (named: "avatar_default_big"))
        }
        
        
    }
    //懒加载 
    //背景图片
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView()
        
//        img.image = UIImage (named: "ad_background")
        
        return img
    }()
    
    private lazy var photoImage: UIImageView = {
        let ima = UIImageView()
        
//        ima.image = UIImage (named: "avatar_default_big")
//        ima.image = UIImage (named: "ad_background")
        
        ima.layer.cornerRadius = 170/4
        
        ima.layer.masksToBounds = true
        
        ima.layer.borderColor = UIColor.grayColor().CGColor
        
        ima.layer.borderWidth = 2
        
        return ima
    }()
    
    private lazy var nameLabel: UILabel = {
        let lab = UILabel ()
        
        lab.textAlignment = .Center
        
        lab.textColor = UIColor.darkGrayColor()
        
        lab.font = UIFont.systemFontOfSize(17)
        
//        if let lab.text = "\(YSUserViewModel.shareInstance.model?.screen_name )欢迎回来 "
        let name = YSUserViewModel.shareInstance.model?.screen_name ?? ""
        
        lab.text = name
        
        
        return lab
    }()
}




















